/*
* ============================================================================
*  Name        : camengtestappui.cpp
*  Part of     : camengtest
*  Description : 
*  Version     : %version: 2 %
*
* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details.
*
* You are welcome to, but not required to, contribute any modifications that
* improve stability, usability and/or feature set of the library back to
* Symbian/S60 Camera Wrapper project, as long as binary compatibility is
* retained. See https://projects.forum.nokia.com/camerawrapper for more info.
* ============================================================================
*/

// INCLUDE FILES
#include <avkon.hrh>
#include <aknnotewrappers.h>
#include <stringloader.h>
#include <CAMENGTEST.rsg>
#include <f32file.h>
#include <s32file.h>

#include "CAMENGTEST.pan"
#include "CAMENGTESTAppUi.h"
#include "CAMENGTESTAppView.h"
#include "CAMENGTEST.hrh"

#include "cameraengine.h"

#include "filelogger.h"

//#define USEFRONTCAMERA


// ============================ MEMBER FUNCTIONS ===============================


// -----------------------------------------------------------------------------
// CCAMENGTESTAppUi::ConstructL()
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void CCAMENGTESTAppUi::ConstructL()
    {
    BaseConstructL(CAknAppUi::EAknEnableSkin);    

    iAppView = CCAMENGTESTAppView::NewL( ClientRect() );
    
    iVfSize = TSize(240,180);
    // Use a semi-random capture size to test if the closest one gets selected
    iImgSize = TSize(650,460);    
    
    if( CCameraEngine::CamerasAvailable() )
      {
#ifdef USEFRONTCAMERA
      iEng = CCameraEngine::NewL(1, 0, this);
#else
      iEng = CCameraEngine::NewL(0, 0, this);
#endif
      iEng->ReserveAndPowerOn();
      }
    else
      {
      iAppView->SetText(_L("No camera found"));
      }
    }
// -----------------------------------------------------------------------------
// CCAMENGTESTAppUi::CCAMENGTESTAppUi()
// C++ default constructor can NOT contain any code, that might leave.
// -----------------------------------------------------------------------------
//
CCAMENGTESTAppUi::CCAMENGTESTAppUi()
: iDoFocus(ETrue)
    {
    // No implementation required
    }

// -----------------------------------------------------------------------------
// CCAMENGTESTAppUi::~CCAMENGTESTAppUi()
// Destructor.
// -----------------------------------------------------------------------------
//
CCAMENGTESTAppUi::~CCAMENGTESTAppUi()
    {
    delete iAppView;
    if(iEng)
      iEng->ReleaseAndPowerOff();
    delete iEng;
    }


// -----------------------------------------------------------------------------
// CCAMENGTESTAppUi::HandleKeyEventL()
// Takes care of key handling.
// -----------------------------------------------------------------------------
//
TKeyResponse CCAMENGTESTAppUi::HandleKeyEventL(
    const TKeyEvent& aKeyEvent,
    TEventCode aType)
  {
  if(aKeyEvent.iCode == EKeyOK)
    {    
    if(iDoFocus && iEng && iEng->IsAutoFocusSupported())
      {
      iAppView->SetText(_L("Focusing..."));
      iEng->StartFocusL();
      }
    else
      {
      HandleCommandL(ECAMENGTESTCommandCapture);
      }
    }
    
    if(iEng && aKeyEvent.iCode == '0')
    {
    HandleCommandL(ECAMENGTESTCommandCapture);
    }
    
  if(iEng && aKeyEvent.iCode == EKeyUpArrow)
    {
    iEng->AdjustDigitalZoom( ETrue );
    }
  
  if(iEng && aKeyEvent.iCode == EKeyDownArrow)
    {
    iEng->AdjustDigitalZoom( EFalse );
    }
  
  
  return EKeyWasNotConsumed;
  }
  
// -----------------------------------------------------------------------------
// CCAMENGTESTAppUi::HandleCommandL()
// Takes care of command handling.
// -----------------------------------------------------------------------------
//
void CCAMENGTESTAppUi::HandleCommandL( TInt aCommand )
    {
    iAppView->GetText().Zero();

    if( aCommand == EEikCmdExit || 
        aCommand == EAknSoftkeyExit )
      {
      Exit();
      return;
      }

    if(!iEng)
      {
      iAppView->SetText(_L("Not ready"));
      return;
      }
      
    switch( aCommand )
        {
        case ECAMENGTESTCommandStartVF:
            {
            iVfSize = TSize(128,96);
            iEng->StartViewFinderL( iVfSize );
            iUsingVF = ETrue;
            }
            break;

        case ECAMENGTESTCommandStopVF:
            {
            iEng->StopViewFinder();
            iAppView->DrawViewFinderFrame( NULL );
            iAppView->SetText(_L("VF Stopped."));            
            iUsingVF = EFalse;
            }
            break;

        case ECAMENGTESTCommandCapture:
            {
            iAppView->SetText(_L("Capturing..."));   
            iEng->StopViewFinder();
            iEng->CaptureL();
            }
            break;
            
        case ECAMENGTESTCommandAF:
            {      
            if(iEng->IsAutoFocusSupported())
              {
              if(iEng->AdvancedSettings())
                {
                iAppView->SetText(_L("AF Supported (CCameraAdvSett)"));
                }
              else
                {
                iAppView->SetText(_L("AF Supported (CCamAutoFocus)"));
                }
              }
            else
              iAppView->SetText(_L(" No AF Support"));              
            }
            break;
            
        case ECAMENGTESTCommandExp:
            {
            // Rotate through supported exposure modes
            if(iExposure == 0)
              iExposure = 1;
            else
              iExposure = iExposure << 1;
              
            if((iEng->SupportedExposureModes() & iExposure) == 0)
              iExposure = 0;
            
            TInt ret = iEng->SetExposure( iExposure );
            
            if(ret == KErrNone)
              iAppView->SetText(_L("Exp. mode %d OK"), iExposure);
            else
              iAppView->SetText(_L("Exp. mode %d failed"), iExposure);              
            }
            break;
            
        case ECAMENGTESTCommandFlash:
            {
            // Rotate through supported flash modes
            if(iFlash == 0)
              iFlash = 1;
            else
              iFlash = iFlash << 1;
            if((iEng->SupportedFlashModes() & iFlash) == 0)
              iFlash = 0;
            
            TInt ret = iEng->SetFlash( iFlash );
            
            if(ret == KErrNone)
              iAppView->SetText(_L("Flash mode %d OK"), iFlash);
            else
              iAppView->SetText(_L("Flash mode %d failed"), iFlash);              
            }
            break;
            
        default:
            break;
        }
    }
// -----------------------------------------------------------------------------
//  Called by framework when layout is changed.
//  Passes the new client rectangle to the AppView
// -----------------------------------------------------------------------------
//
void CCAMENGTESTAppUi::HandleResourceChangeL( TInt aType )
{
    CAknAppUi::HandleResourceChangeL(aType);
    if (aType == KEikDynamicLayoutVariantSwitch)
        {
        if (iAppView)
            iAppView->SetRect( ClientRect() );
        }
}

void CCAMENGTESTAppUi::MceoCameraReady()
  {

#ifdef USEFRONTCAMERA
  iEng->PrepareL( iImgSize, CCamera::EFormatFbsBitmapColor16M );
#else
  iEng->PrepareL( iImgSize );
#endif

  TBuf<64> readyMsg;
  readyMsg.Format( _L("Ready (%dx%d), AF: "), iImgSize.iWidth, iImgSize.iHeight );
  readyMsg.Append( iEng->IsAutoFocusSupported() ? _L("Enabled") : _L("n/a") );
  iAppView->SetText( readyMsg );
  iEng->StartViewFinderL( iVfSize );
  
  __LOGSTR_TOFILE1("Available cameras: %d", iEng->CamerasAvailable());
  __LOGSTR_TOFILE1("Supported exp. modes: %x", iEng->SupportedExposureModes());
  __LOGSTR_TOFILE1("Supported flash modes: %x", iEng->SupportedFlashModes());
  iUsingVF = ETrue;
  }

void CCAMENGTESTAppUi::MceoFocusComplete()
  {
   iAppView->SetText(_L("Focused"));
  //HandleCommandL( ECAMENGTESTCommandCapture );
  }
  
  
void CCAMENGTESTAppUi::MceoCapturedDataReady( TDesC8* aData )
  {
  iAppView->SetText(_L("Captured %dkB OK"), aData->Length() >> 10);
  
  // TODO: Do something with the captured data...
  
  iEng->ReleaseImageBuffer();
  
  // Continue VF
  if(iUsingVF)
    {
    iEng->StartViewFinderL(iVfSize);
    }
  }
  
void CCAMENGTESTAppUi::MceoHandleError( TCameraEngineError aErrorType, TInt aError )
  {
  //iAppView->SetText(_L("Error %d (%d)"), aErrorType, aError);
  __LOGSTR_TOFILE2("Error %d (%d)", aErrorType, aError);
  }
  
void CCAMENGTESTAppUi::MceoCapturedBitmapReady( CFbsBitmap* aBitmap )
  {
  iAppView->SetText(_L("Bitmap captured (%dkB)"), aBitmap->Header().iBitmapSize >> 10);
  
  iEng->ReleaseImageBuffer();
  // Continue VF
  if(iUsingVF)
    {
    iEng->StartViewFinderL(iVfSize);
    }
  }
  
void CCAMENGTESTAppUi::MceoViewFinderFrameReady( CFbsBitmap& aFrame )
  {
  iAppView->DrawViewFinderFrame( &aFrame );
  iEng->ReleaseViewFinderBuffer();
  }
  
void CCAMENGTESTAppUi::MceoHandleOtherEvent( const TECAMEvent& aEvent )
  {
  __LOGSTR_TOFILE2("[%x, %d]", aEvent.iEventType, aEvent.iErrorCode);
  }
     
// End of File
